package com.infore.authorityManagement.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.infore.authorityManagement.entity.BusinessConstant;
import com.infore.authorityManagement.entity.SysUser;
import com.infore.authorityManagement.enums.ResultStatus;
import com.infore.authorityManagement.service.ISysUserService;
import com.infore.authorityManagement.util.ResultEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(description = "用户认证")
@Controller
@RequestMapping("/auth")
public class AuthController {
    
    @Autowired
    ISysUserService userService;
    
    @ApiOperation(value = "用户登录", notes = "填写用户详细信息, 用户账号(user_account)、密码(password)必须填写，返回token")
    @ApiImplicitParams({@ApiImplicitParam(name = "account", value = "用户账号", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appUUID", value = "手机端参数", paramType = "query", required = true, dataType = "String"), })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody ResultEntity userAuthentication(@RequestParam String account, @RequestParam String password,
            HttpServletRequest request) {
        /*logger.info(" 用户Id:" + request.getHeader("userId") + " 设备唯一标识:" + request.getHeader("uuid") + " 平台：" + request.getHeader("platform")
                + "App版本：" + request.getHeader("appVersion") + " 设备型号：" + request.getHeader("device") + " 网络:" + request.getHeader("net")
                + "登录");*/
        String appUUID = request.getHeader("appUUID");
        Map<String, Object> map = userService.userAuth(account, password, appUUID, request);
        int resultIdt = (int) map.get("resultIdt");
        if (resultIdt == -1) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.USER_NOT_EXIST, null);
        } else if (resultIdt == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.AUTH_FAILURE_PASSWORDNO, null);
        } else if (resultIdt == -3) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.AUTH_FAILURE_ACCOUNT, null);
        } else {
            String token = map.get("token").toString();
            SysUser userInfo = (SysUser) map.get("userInfo");
            return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.AUTH_SUCCESS, null).setInfo(userInfo).setMapValue("token", token);
        }
    }
    
    @ApiOperation(value = "退出系统", notes = "用户退出，注销token")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public @ResponseBody ResultEntity logout(String token) {
        int result = userService.logout(token);
        if (result == -1 || result == -2) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.AUTH_FAILURE_TOKEN, null);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.LOGOUT_SUCCESS, null);
    }
    
    @ApiOperation(value = "验证token", notes = "验证token")
    @RequestMapping(value = "/validateToken", method = RequestMethod.GET)
    public @ResponseBody ResultEntity validateToken(String token) {
        int result = userService.validateToken(token);
        if (result == -1 || result == -2 || result == -3) {
            return new ResultEntity(ResultStatus.ERROR, BusinessConstant.AUTH_FAILURE_TOKEN, null);
        }
        return new ResultEntity(ResultStatus.SUCCESS, BusinessConstant.VALID_TOKEN_SUCCESS, null);
    }
    
}
